<?php

CRequireIfPHP4('CObject');

/**
A safe-to-copy structure that contains extended information on the current
request, as sent by Second Life simulators on a llHTTPRequest() call.

Items not sent by the simulator will be null (and all items will be null if the request does not come from Second Life).
*/
class SLInfo extends CObject {
	/** The shard this request was coming from. The main grid returns a string similar to "Production", while the beta grid may return a different string (like "aditi" or "beta"). */
	var $Shard = null;
	
	/** The name of the object whose script made the request. */
	var $ObjectName = null;
	/** The GUID of the object whose script made the request as a string. */
	var $ObjectKey = null;
	
	/** The region string returned by the Second Life server, including extra coordinates. This is the region where the object whose script made the request is in. */
	var $Region = null;
	/** The name of the region where the object whose script made the request is in. */
	var $RegionName = null;
	/** The position of the object whose script made the request as an array('x' => ..., 'y' => ..., 'z' => ...) */
	var $LocalPosition = null;
	/** The rotation of the object whose script made the request as an array('x' => ..., 'y' => ..., 'z' => ..., 'w' => ...) */	
	var $LocalRotation = null;
	/** The velocity vector of the object whose script made the request as an array('x' => ..., 'y' => ..., 'z' => ...) */
	var $LocalVelocity = null;
	
	/** The owner of the object whose script made the request, as a string in the format "Name Surname" */
	var $OwnerName = null;
	/** The GUID of the owner of the object whose script made the request as a string. */
	var $OwnerKey = null;

	function Init() {
		if (!SLIsSecondLife())
			return;
		
		$this->Shard = @$_SERVER['HTTP_X_SECONDLIFE_SHARD'];
		$this->ObjectName = @$_SERVER['HTTP_X_SECONDLIFE_OBJECT_NAME'];
		$this->ObjectKey = @$_SERVER['HTTP_X_SECONDLIFE_OBJECT_KEY'];
		$this->Region = @$_SERVER['HTTP_X_SECONDLIFE_REGION'];
		$this->OwnerName = @$_SERVER['HTTP_X_SECONDLIFE_OWNER_NAME'];
		$this->OwnerKey = @$_SERVER['HTTP_X_SECONDLIFE_OWNER_KEY'];

		if (!is_null($this->Region)) {
			if (($i = strpos($this->Region, ' ')) !== false)
				$this->RegionName = substr($this->Region, 0, $i);
		}

		$regexp3 = '/\((\d+(\.\d+)?)\s*,\s*(\d+(\.\d+)?)\s*,\s*(\d+(\.\d+)?)\)/';
		if (isset($_SERVER['HTTP_X_SECONDLIFE_LOCAL_POSITION']) &&
			preg_match($regexp3, $_SERVER['HTTP_X_SECONDLIFE_LOCAL_POSITION'], $matches))
			$this->LocalPosition = array(
				'x' => (double) $matches[1],
				'y' => (double) $matches[3],
				'z' => (double) $matches[5]);
		
		if (isset($_SERVER['HTTP_X_SECONDLIFE_LOCAL_VELOCITY']) &&
			preg_match($regexp3, $_SERVER['HTTP_X_SECONDLIFE_LOCAL_VELOCITY'], $matches))
			$this->LocalVelocity = array(
				'x' => (double) $matches[1],
				'y' => (double) $matches[3],
				'z' => (double) $matches[5]);

		$regexp4 = '/\((\d+(\.\d+)?)\s*,\s*(\d+(\.\d+)?)\s*,\s*(\d+(\.\d+)?)\s*,\s*(\d+(\.\d+)?)\)/';		
		if (preg_match($regexp4, $_SERVER['HTTP_X_SECONDLIFE_LOCAL_ROTATION'], $matches))
			$this->LocalRotation = array(
				'x' => (double) $matches[1],
				'y' => (double) $matches[3],
				'z' => (double) $matches[5],
				'w' => (double) $matches[7]);
	}
}

?>